<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui表格示例</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/layui@2.6.8/dist/css/layui.css">
</head>
<body>
<div class="layui-container">
    <div class="layui-card">
        <div class="layui-card-header">产品管理</div>
        <div class="layui-card-body">
            <div class="layui-form">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <input type="text" id="searchKey" placeholder="搜索名称" class="layui-input">
                    </div>
                    <button class="layui-btn" id="btnSearch">搜索</button>
                    <button class="layui-btn layui-btn-normal" id="btnAdd">添加</button>
                </div>
            </div>

            <table id="productTable" lay-filter="productTable"></table>
        </div>
    </div>
</div>

<!-- 添加/编辑表单模板 -->
<script type="text/html" id="formTemplate">
    <form class="layui-form" lay-filter="productForm" style="padding:20px">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">产品名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入名称"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-block">
                <input type="number" name="price" step="0.01" required lay-verify="required|number"
                       placeholder="请输入价格" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库存</label>
            <div class="layui-input-block">
                <input type="number" name="stock" required lay-verify="required|number" placeholder="请输入库存"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit">提交</button>
            </div>
        </div>
    </form>
</script>

<!-- 操作列模板 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="https://cdn.jsdelivr.net/npm/layui@2.6.8/dist/layui.js"></script>
<script>
    layui.use(['table', 'form', 'layer'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.$;

        // 初始化表格
        var tableIns = table.render({
            elem: '#productTable',
            url: '/fspl_crud/api/products',
            method: 'get',
            page: true,
            cols: [[
                {field: 'id', title: 'ID', width: 80, sort: true},
                {field: 'name', title: '产品名称'},
                {field: 'price', title: '价格', sort: true},
                {field: 'stock', title: '库存', sort: true},
                {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 150}
            ]]
        });

        // 搜索功能
        $('#btnSearch').click(function () {
            var key = $('#searchKey').val();
            tableIns.reload({
                where: {key: key},
                page: {curr: 1}
            });
        });

        // 添加按钮
        $('#btnAdd').click(function () {
            showForm({});
        });

        // 表格工具栏事件
        table.on('tool(productTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确定删除吗？', function (index) {
                    $.ajax({
                        url: '/fspl_crud/api/product/' + data.id,
                        type: 'DELETE',
                        success: function () {
                            layer.msg('删除成功');
                            tableIns.reload();
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                showForm(data);
            }
        });

        // 显示表单弹窗
        function showForm(data) {
            var title = data.id ? '编辑产品' : '添加产品';
            var index = layer.open({
                type: 1,
                title: title,
                content: $('#formTemplate').html(),
                area: ['500px', '400px'],
                success: function () {
                    form.val('productForm', data);
                }
            });

            // 表单提交
            form.on('submit(formSubmit)', function (formData) {
                var url = formData.field.id ? '/fspl_crud/api/product/' + formData.field.id : '/fspl_crud/api/product';
                var method = formData.field.id ? 'PUT' : 'POST';

                $.ajax({
                    url: url,
                    type: method,
                    contentType: 'application/json',
                    data: JSON.stringify(formData.field),
                    success: function () {
                        layer.close(index);
                        layer.msg('操作成功');
                        tableIns.reload();
                    },
                    error: function (xhr) {
                        layer.msg('操作失败: ' + xhr.responseJSON.msg);
                    }
                });
                return false;
            });
        }
    });
</script>
</body>
</html>
